//
//  CarContacts.swift
//  Automobile
//
//  Created by erkebulan elzhan on 8/20/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit

final class CarContactsCell: UITableViewCell {

    // MARK: - Outlets

    private lazy var nameField: ModalElement = {
        let element = ModalElement()
        element.updateUI(value: nil, placeholder: "Имя", withTF: true, keyboardType: .asciiCapable)
        return element
    }()
    
    private lazy var emailField: ModalElement = {
        let element = ModalElement()
        element.updateUI(value: nil, placeholder: "Почта", withTF: true, keyboardType: .emailAddress)
        return element
    }()
    
    private lazy var cityBtn: ModalElement = {
        let element = ModalElement()
        element.updateUI(value: nil, placeholder: "Город продажи")
        element.delegate = self
        return element
    }()
    
    private lazy var buttonsStack: UIStackView = {
        let stack = UIStackView()
        stack.axis = .vertical
        stack.alignment = .fill
        stack.distribution = .fillEqually
        stack.spacing = 12
        return stack
    }()
    

    private lazy var titleLbl: UILabel = {
        let lbl = UILabel()
        lbl.font = UIFont.systemFont(ofSize: 17, weight: .bold)
        lbl.textAlignment = .left
        lbl.sizeToFit()
        lbl.textColor = .black
        lbl.text = "Контакты"
        return lbl
    }()
    
    
    // MARK: - Actions
    
    @objc private func setRegion (notification: NSNotification) {
        if let userInfo = notification.userInfo, let region = userInfo["region"] as? City {
            self.cityBtn.updateUI(value: region.name)
        }
    }
    
    
    
    // MARK: - Init
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.addViews()
        NotificationCenter.default.addObserver(self, selector: #selector(setRegion(notification:)), name: NSNotification.Name("setRegion"), object: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name("setRegion"), object: nil)
    }
    
}

extension CarContactsCell: ConfigurableCell {
    
    func configure(data object: MainViewModel.CarContacts?, index: Int) {
        if let contactData = object {
            nameField.setTextFieldText(contactData.name)
            emailField.setTextFieldText(contactData.email)
            cityBtn.updateUI(value: contactData.city?.name)
        }
    }

    func returnData() -> MainViewModel.CarContacts? {
        return MainViewModel.CarContacts(city: nil, name: self.nameField.getTextFieldValue(), email: self.emailField.getTextFieldValue())
    }
}



extension CarContactsCell {
    
    private func addViews () {
        self.contentView.addSubview(buttonsStack)
        self.contentView.addSubview(titleLbl)
        buttonsStack.addArrangedSubview(nameField)
        buttonsStack.addArrangedSubview(emailField)
        buttonsStack.addArrangedSubview(cityBtn)
        
        let itemsCount = buttonsStack.arrangedSubviews.count
        
        titleLbl.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(16)
            make.top.equalToSuperview().offset(22)
            make.height.equalTo(20)
        }
        
        buttonsStack.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(16)
            make.height.equalTo(itemsCount * 48 + (itemsCount - 1) * 12)
            make.bottom.equalToSuperview().inset(16)
            make.top.equalTo(titleLbl.snp.bottom).offset(12)
        }

    }
}

extension CarContactsCell: ModalElementProtocol {
    func selectElement(modal: ModalView.ModalType?, customID: Int?) {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "contactsUpdate"), object: nil, userInfo: ["isSelected": true])
    }
    
    func didClear(modal: ModalView.ModalType) {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "contactsUpdate"), object: nil, userInfo: ["isSelected": false])
    }
}


